﻿namespace = culture_emergence_events

# Opportunity for your culture to hybridize with your capital culture
culture_emergence_events.0001 = {
	type = character_event
	title = culture_emergence_events.0001.t
	desc = culture_emergence_events.0001.desc
	theme = realm
	override_background = {
		reference = terrain_scope
	}
	left_portrait = {
		character = root
		animation = personality_rational
	}
	
	trigger = {
		has_dlc_feature = hybridize_culture
		NOT = { government_has_flag = government_is_herder }
		NAND = {
			is_ai = yes
			always = yes
#			has_game_rule = none_hybrid_culture_ai_frequency
		}
		NAND = {
			is_ai = yes
			exists = global_var:has_ai_hybrid_event_cooldown
		}
		is_independent_ruler = yes
		exists = capital_county
		capital_county = {
			holder = root
			NOT = {
				culture = root.culture
			}
			culture = {
				OR = {
					cultural_acceptance = { target = root.culture value >= hybridization_threshold_flat_number_value }
					root.culture = {
						has_cultural_parameter = easier_to_hybridize
					}
				}
			}
		}
		culture = {
			NOT = { has_same_culture_heritage = root.capital_county.culture }
			cultural_acceptance = { target = root.capital_county.culture value >= hybridization_threshold_value }
			culture_age >= culture_hybrid_cooldown
		}
		OR = {
			NOT = {
				any_sub_realm_county = {
					culture = root.culture
				}
			}
			any_sub_realm_county = {
				culture = root.culture
				any_neighboring_county = {
					culture = root.capital_county.culture
				}
			}
			AND = {
				culture = {
					has_cultural_parameter = easier_to_hybridize
				}
				any_sub_realm_county = {
					culture = root.culture
					title_province = {
						is_coastal = yes
					}
				}
				any_sub_realm_county = {
					culture = root.capital_county.culture
					title_province = {
						is_coastal = yes
					}
				}
			}
		}
		OR = {
			realm_size <= 6
			any_sub_realm_county = {
				percent >= 0.7
				culture = root.capital_county.culture
			}
			AND = {
				any_sub_realm_county = {
					percent <= 0.3
					NOR = {
						culture = root.capital_county.culture
						culture = root.culture
					}
				}
				any_sub_realm_county = {
					percent >= 0.2
					culture = root.capital_county.culture
				}
				any_sub_realm_county = {
					percent >= 0.2
					culture = root.culture
				}
			}
		}
		NOR = {
			culture = {
				has_cultural_parameter = harder_to_hybridize
			}
			capital_county.culture = {
				has_cultural_parameter = harder_to_hybridize
			}
			faith.religious_head = root
			AND = {
				culture = {
					is_hybrid_culture = yes
				}
				capital_county.culture = {
					is_hybrid_culture = yes
				}
			}
			AND = { # We don't want the AI to spawn ugly hybrid cultures willy-nilly, fine for the player though
				is_ai = yes
				NOT = {
					has_game_rule = very_relaxed_hybrid_culture_ai_restrictions
				}
				culture = {
					is_hybrid_culture = yes
					culture_is_not_historical_hybrid_trigger = yes
				}
			}
			AND = { # We don't want the AI to spawn ugly hybrid cultures willy-nilly, fine for the player though
				is_ai = yes
				NOT = {
					has_game_rule = very_relaxed_hybrid_culture_ai_restrictions
				}
				capital_county.culture = {
					is_hybrid_culture = yes
					culture_is_not_historical_hybrid_trigger = yes
				}
				culture = {
					culture_is_not_historical_hybrid_trigger = yes
				}
			}
			capital_county.culture = {
				is_hybrid_culture = yes
				any_parent_culture = {
					OR = {
						this = root.culture
						has_same_culture_heritage = root.culture
					}
				}
			}
			root.culture = {
				is_hybrid_culture = yes
				any_parent_culture = {
					OR = {
						this = root.capital_county.culture
						has_same_culture_heritage = root.capital_county.culture
					}
				}
			}
		}
	}
	
	weight_multiplier = {
		base = 1
		modifier = {
			add = 2
			root.culture = {
				has_cultural_parameter = easier_to_hybridize
			}
		}
		modifier = {
			add = 4
			root.culture = {
				has_cultural_parameter = easier_to_hybridize
			}
			root.culture = culture:norse
			root.capital_county = {
				title_province = { geographical_region = world_europe_west_britannia }
			}
		}
		modifier = {
			add = 2
			has_trait = adventurer
		}
		modifier = {
			factor = 0.2
			is_ai = yes
			has_game_rule = less_common_hybrid_culture_ai_frequency
		}
		modifier = {
			add = 5
			root.capital_county = {
				culture = { has_cultural_parameter = other_cultures_more_likely_to_hybridize }
			}
		}
	}

	immediate = {
		# Global cooldowns for the lower frequency game rule settings
		if = {
			limit = {
				is_ai = yes
				has_game_rule = less_common_hybrid_culture_ai_frequency
			}
			set_global_variable = { name = has_ai_hybrid_event_cooldown value = yes days = 3650 }
		}
		root = {
			save_scope_as = hybridizer
			culture = { save_scope_as = hybridizer_old_culture }
		}
		capital_county = {
			save_scope_as = capital_county_scope
		}
		capital_county.culture = {
			save_scope_as = hybdridization_offer_culture
		}
		
		if = { # If a hybrid culture already exists
			limit = {
				any_in_global_list = {
					variable = hybrid_cultures
					any_parent_culture = { this = root.culture }
					any_parent_culture = { this = root.capital_county.culture }
				}
			}
			random_in_global_list = {
				variable = hybrid_cultures
				limit = {
					any_parent_culture = { this = root.culture }
					any_parent_culture = { this = root.capital_county.culture }
				}
				save_scope_as = existing_hybrid_culture
			}
		}
	}

	option = {
		name = culture_emergence_events.0001.a
		
		flavor = new_hybrid_culture_tt
		
		if = { # If a hybrid culture already exist, convert to that one
			limit = {	
				exists = scope:existing_hybrid_culture
			}
			custom_tooltip = culture_emergence_events.0001.existing_hybrid
			custom_tooltip = culture_emergence_events.vassals
			hidden_effect = {
				new_culture_created_vassal_conversion_effect = {
					CONVERTEE = root
					CONVERTER = root
					CULTURE = scope:existing_hybrid_culture
				}
			}
		}
		else = {
			custom_tooltip = culture_emergence_events.0001.new_hybrid
			custom_tooltip = culture_emergence_events.vassals
			hidden_effect = {
				create_hybrid_culture = scope:hybdridization_offer_culture
			}
		}
		
		# For testing
		#add_to_global_variable_list = { name = hybrid_cultures_from_events target = root.culture }
		#if = {
		#	limit = {
		#		any_in_global_list = {
		#			variable = hybrid_cultures_from_events # Error suppression
		#		}
		#	}
		#}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = culture_emergence_events.0001.b
		
		scope:hybdridization_offer_culture = {
			change_cultural_acceptance = {
				target = root.culture
				value = 5
				desc = cultural_acceptance_gain_event
			}
		}

		ai_chance = {
			base = 0
		}
	}
	
	after = {
		if = {
			limit = {
				exists = domicile.domicile_culture
				domicile.domicile_culture = scope:hybridizer_old_culture
				domicile.domicile_culture != scope:hybridizer.culture
			}
			domicile = {
				set_domicile_culture = scope:hybridizer.culture
			}
			every_held_title = {
				limit = {
					tier = tier_county
					culture = scope:hybridizer_old_culture
				}
				set_county_culture = scope:hybridizer.culture
			}
		}
	}
}

# Event for your culture to diverge
culture_emergence_events.0002 = {
	type = character_event
	title = culture_emergence_events.0002.t
	desc = culture_emergence_events.0002.desc
	theme = realm
	override_background = {
		reference = terrain_scope
	}
	left_portrait = {
		character = root
		animation = happiness
	}
	
	trigger = {
		has_dlc_feature = diverge_culture
		is_ai = no
		exists = capital_county
		capital_county = {
			culture = root.culture
		}
		primary_title = { # We don't want 'Caliphal' or 'Papal' culture appearing :D
			any_in_de_jure_hierarchy = {
				tier = tier_county
			}
			tier >= tier_duchy
		}
		culture = {
			culture_age >= culture_divergence_cooldown
			culture_number_of_counties >= 15
		}
		NOT = {
			culture = {
				has_variable = recent_event_divergence
			}
		}
		any_sub_realm_county = {
			count >= 5
			culture = root.culture
		}
		NOT = {
			culture = {
				any_culture_county = {
					percent >= 0.5
					holder = {
						OR = {
							this = root
							is_vassal_or_below_of = root
						}
					}
				}
			}
		}
		NOT = {
			faith.religious_head = root
		}
	}
	
	weight_multiplier = {
		base = 1
		modifier = {
			add = 1
			root.culture = {
				culture_number_of_counties >= 70
			}
		}
		modifier = {
			add = 1
			root.culture = {
				culture_number_of_counties >= 60
			}
		}
		modifier = {
			add = 1
			root.culture = {
				culture_number_of_counties >= 50
			}
		}
		modifier = {
			add = 1
			root.culture = {
				culture_number_of_counties >= 40
			}
		}
		modifier = {
			add = 1
			root.culture = {
				culture_number_of_counties >= 30
			}
		}
		modifier = {
			add = 1
			has_game_rule = relaxed_divergent_culture_ai_frequency
		}
		modifier = {
			factor = 0.1
			NOT = {
				has_game_rule = relaxed_divergent_culture_ai_frequency
			}
			root = {
				is_independent_ruler = no
				top_liege.culture = root.culture
			}
		}
		modifier = {
			factor = 0.2
			is_ai = yes
			has_game_rule = less_common_divergent_culture_ai_frequency
		}
		modifier = { # Covered by the Norse Culture Split
			factor = 0
			root = {
				culture = culture:norse
				capital_county = {
					title_province = {
						OR = {
							geographical_region = custom_danish
							geographical_region = custom_norwegian
							geographical_region = custom_fennoscandia
						}
					}
				}
			}
		}
	}

	immediate = {
		root = {
			save_scope_as = diverger
			culture = {
				save_scope_as = diverger_old_culture
			}
		}
		capital_county = {
			save_scope_as = capital_county_scope
		}
		capital_county.culture = {
			save_scope_as = divergence_offer_culture
		}
	}

	option = {
		name = culture_emergence_events.0002.a
		
		flavor = new_divergent_culture_tt
		
		custom_tooltip = culture_emergence_events.0002.new_divergence
		custom_tooltip = culture_emergence_events.vassals
		hidden_effect = {
			create_divergent_culture_with_side_effects_excluding_cost = yes
		}
		
		culture = {
			set_variable = {
				name = recent_event_divergence
				#value = true
				days = 3650
			}
		}
		
		# For testing
		#add_to_global_variable_list = { name = diverged_cultures_from_events target = root.culture }
		#if = {
		#	limit = {
		#		any_in_global_list = {
		#			variable = diverged_cultures_from_events # Error suppression
		#		}
		#	}
		#}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = culture_emergence_events.0002.b
		
		add_dread = medium_dread_gain
		
		ai_chance = {
			base = 0
		}
	}
	
	after = {
		if = {
			limit = {
				exists = domicile.domicile_culture
				domicile.domicile_culture = scope:diverger_old_culture
				domicile.domicile_culture != scope:diverger.culture
			}
			domicile = {
				set_domicile_culture = scope:diverger.culture
			}
			every_held_title = {
				limit = {
					tier = tier_county
					culture = scope:diverger_old_culture
				}
				set_county_culture = scope:diverger.culture
			}
		}
	}
}

scripted_trigger valid_county_to_usurp = {
	exists = holder
	holder = {
		is_ai = yes
		NOR = {
			liege = {
				is_ai = no
			}
			any_close_or_extended_family_member = {
				is_ai = no
			}
		}
	}
}

scripted_trigger holder_valid_to_diverge = {
	NOT = {
		any_sub_realm_county = {
			culture = scope:culture_check_2
			NOT = {
				duchy = scope:duchy_title
			}
		}
	}
	primary_title = { # We don't want 'Caliphal' or 'Papal' culture appearing :D
		any_in_de_jure_hierarchy = {
			tier = tier_county
		}
	}
	trigger_if = {
		limit = {
			exists = faith.religious_head
		}
		NOT = {
			this = faith.religious_head
		}
	}
	culture = scope:culture_check_2
	capital_county = {
		duchy = scope:duchy_title
	}
}

scripted_trigger holder_valid_to_diverge_except_culture = {
	NOT = {
		any_sub_realm_county = {
			NOT = {
				duchy = scope:duchy_title
			}
		}
	}
	capital_county = {
		duchy = scope:duchy_title
	}
}

culture_emergence_events.0003 = { # Emergent Divergences, based on adjacency to other cultures, or insular isolation
	#type = character_event
	#title = culture_emergence_events.0002.t
	#desc = culture_emergence_events.0003.desc
	#theme = realm
	#left_portrait = {
	#	character = scope:diverge_dummy
	#	animation = happiness
	#}
	
	scope = culture
	hidden = yes
	
	trigger = {
		has_dlc_feature = diverge_culture
		NOT = {
			always = yes
#			has_game_rule = none_divergent_culture_ai_frequency
		}
		NOT = { exists = global_var:has_ai_divergence_event_cooldown }
		save_temporary_scope_as = culture_check
		culture_age >= culture_divergence_cooldown
		culture_number_of_counties >= 15
		any_culture_duchy = {
			any_in_de_jure_hierarchy = {
				count >= 2
				continue = { tier = tier_duchy }
				tier = tier_county
				OR = {
					any_neighboring_county = {
						count >= 2
						NOT = {
							culture = scope:culture_check
						}
						culture = {
							culture_age >= culture_divergence_cooldown
						}
					}
					title_province = { # Islands can always diverge
						geographical_region = divergence_island_regions
					}
				}
				culture = scope:culture_check
				valid_county_to_usurp = yes
			}
			NAND = { # The Cultural Heads capital duchy is protected
				exists = scope:culture_check.culture_head
				any_in_de_jure_hierarchy = {
					continue = { tier = tier_duchy }
					tier = tier_county
					holder = scope:culture_check.culture_head
					this = holder.capital_county
				}
			}
		}
	}

	immediate = {
		# Note from the on-action, we have a 10% chance to get here if we meet the criteria
		random = { # Find a culture that fits the criteria for divergence
			chance = { # Prefer bigger cultures
				value = {
					value = culture_number_of_counties
					subtract = 10
					# These are *after* the initial 10% chance
					# At 15, we get a 5 in 100 chance. MTTH: 600 years
					# At 30, we get a 20 in 100 chance. MTTH: 150 years
					# At 50, we get a 40 in 100 chance. MTTH: 75 years
					# At 110, we get a 100 in 100 chance. MTTH: 30 years
				}
				if = { # This monstrosity ensures that cultures that are contained within large, powerful realms where their liege is their culture don't diverge too often. Example; Byzantium. Greek won't start diverging 'for real' until it starts shattering.
					multiply = 0.025
					limit = {
						save_temporary_scope_as = culture_weight_check
						NOT = {
							has_game_rule = relaxed_divergent_culture_ai_frequency
						}
						NOT = {
							any_culture_duchy = {
								any_in_de_jure_hierarchy = {
									count >= 2
									continue = { tier = tier_duchy }
									tier = tier_county
									OR = {
										any_neighboring_county = {
											count >= 2
											NOT = {
												culture = scope:culture_weight_check
											}
											culture = {
												culture_age >= culture_divergence_cooldown
											}
										}
										title_province = { # Islands can always diverge
											geographical_region = divergence_island_regions
										}
									}
									culture = scope:culture_weight_check
									valid_county_to_usurp = yes
									holder = {
										OR = {
											AND = {
												highest_held_title_tier <= tier_duchy
												is_independent_ruler = yes
											}
											AND = {
												is_independent_ruler = no
												NOT = {
													culture = top_liege.culture
												}
											}
										}
									}
								}
							}
						}
					}
				}
				if = { # Covered by the Norse Culture Split
					multiply = 0
					limit = {
						this = culture:norse
						any_culture_county = {
							title_province = {
								OR = {
									geographical_region = custom_danish
									geographical_region = custom_norwegian
									geographical_region = custom_fennoscandia
								}
							}
						}
					}
				}

				if = {
					limit = {
						has_game_rule = less_common_divergent_culture_ai_frequency
					}
					multiply = 0.2
				}
				if = {
					limit = {
						has_game_rule = relaxed_divergent_culture_ai_frequency
					}
					multiply = 1.25
				}
			}
			
			# Global cooldowns for the lower frequency game rule settings
			if = {
				limit = {
					has_game_rule = less_common_divergent_culture_ai_frequency
				}
				set_global_variable = { name = has_ai_divergence_event_cooldown value = yes days = 3650 }
			}

			save_scope_as = culture_check_2 # Save the culture we want to use below
			random_culture_duchy = { # Find an appropriate duchy
				limit = {
					any_in_de_jure_hierarchy = {
						count >= 2
						continue = { tier = tier_duchy }
						tier = tier_county
						OR = {
							any_neighboring_county = {
								count >= 2
								NOT = {
									culture = scope:culture_check_2
								}
								culture = {
									culture_age >= culture_divergence_cooldown
								}
							}
							title_province = { # Islands can always diverge
								geographical_region = divergence_island_regions
							}
						}
						culture = scope:culture_check_2
						valid_county_to_usurp = yes
					}
					NAND = { # The Cultural Heads capital duchy is protected
						exists = scope:culture_check_2.culture_head
						any_in_de_jure_hierarchy = {
							continue = { tier = tier_duchy }
							tier = tier_county
							holder = scope:culture_check_2.culture_head
							this = holder.capital_county
						}
					}
				}
				weight = { # Independent self-contained dukes are the most logical targets for divergence
					base = 1
					modifier = {
						add = 1000
						any_in_de_jure_hierarchy = {
							count >= 2
							tier = tier_county
							save_temporary_scope_as = county_check_weight
							OR = {
								any_neighboring_county = {
									count >= 2
									NOT = {
										culture = scope:culture_check_2
									}
									culture = {
										culture_age >= culture_divergence_cooldown
									}
								}
								title_province = { # Islands can always diverge
									geographical_region = divergence_island_regions
								}
							}
							culture = scope:culture_check_2
							exists = holder
							holder = {
								NOT = {
									any_sub_realm_county = {
										NOT = {
											duchy = scope:county_check_weight.duchy
										}
									}
								}
								capital_county = {
									duchy = scope:county_check_weight.duchy
								}
								is_independent_ruler = yes
								highest_held_title_tier <= tier_duchy
							}
							valid_county_to_usurp = yes
						}
					}
					modifier = { # Small kings are second best
						add = 25
						any_in_de_jure_hierarchy = {
							count >= 2
							save_temporary_scope_as = county_check_weight
							tier = tier_county
							OR = {
								any_neighboring_county = {
									count >= 2
									NOT = {
										culture = scope:culture_check_2
									}
									culture = {
										culture_age >= culture_divergence_cooldown
									}
								}
								title_province = { # Islands can always diverge
									geographical_region = divergence_island_regions
								}
							}
							culture = scope:culture_check_2
							exists = holder
							holder = {
								NOT = {
									any_sub_realm_county = {
										NOT = {
											duchy = scope:county_check_weight.duchy
										}
									}
								}
								capital_county = {
									duchy = scope:county_check_weight.duchy
								}
								is_independent_ruler = yes
							}
							valid_county_to_usurp = yes
						}
					}
					modifier = { # French and Persian start in very precarious circumstances, give them some time to consolidate
						factor = 0
						OR = {
							scope:culture_check_2 = culture:french
							scope:culture_check_2 = culture:persian
						}
						game_start_date >= 1066.10.15
						years_from_game_start < 20
					}
					modifier = { # Try to keep greek unity within Byzantium
						factor = 0
						scope:culture_check_2 = culture:greek
						any_in_de_jure_hierarchy = {
							exists = holder.top_liege
							holder.top_liege = {
								culture = {
									has_cultural_pillar = heritage_byzantine
								}
								has_title = title:e_byzantium
							}
						}
					}
				}
				# Start preparing the divergence
				save_scope_as = duchy_title
				if = { # First prio; find a contained ruler with the correct culture
					limit = {
						any_in_de_jure_hierarchy = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge = yes
							}
							valid_county_to_usurp = yes
						}
					}
					random_in_de_jure_hierarchy = {
						limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge = yes
								is_independent_ruler = yes
								highest_held_title_tier = tier_duchy
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge = yes
								is_independent_ruler = yes
								highest_held_title_tier <= tier_duchy
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge = yes
								is_independent_ruler = yes
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge = yes
								highest_held_title_tier = tier_duchy
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge = yes
							}
							valid_county_to_usurp = yes
						}
						holder = {
							save_scope_as = diverge_dummy
						}
					}
				}
				else_if = { # Second prio; someone contained who is NOT of the culture (we make them switch)
					limit = {
						any_in_de_jure_hierarchy = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge_except_culture = yes
							}
							valid_county_to_usurp = yes
						}
					}
					random_in_de_jure_hierarchy = {
						limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge_except_culture = yes
								is_independent_ruler = yes
								primary_title.tier = tier_duchy
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge_except_culture = yes
								is_independent_ruler = yes
								highest_held_title_tier <= tier_duchy
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge_except_culture = yes
								is_independent_ruler = yes
							}
							valid_county_to_usurp = yes
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							exists = holder
							holder = {
								holder_valid_to_diverge_except_culture = yes
							}
							valid_county_to_usurp = yes
						}
						holder = {
							set_culture = scope:culture_check_2
							save_scope_as = diverge_dummy
						}
					}
				}
				else = { # Prio 3, the most disruptive alternative - spawn a peasant leader, have them steal a county and diverge
					random_in_de_jure_hierarchy = {
						limit = {
							tier = tier_county
							culture = scope:culture_check_2
							valid_county_to_usurp = yes
							save_temporary_scope_as = county_check
							holder = {
								highest_held_title_tier = tier_county
							}
							NOT = {
								holder = {
									capital_county = scope:county_check
								}
							}
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							valid_county_to_usurp = yes
							save_temporary_scope_as = county_check
							NOT = {
								holder = {
									capital_county = scope:county_check
								}
							}
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							valid_county_to_usurp = yes
							save_temporary_scope_as = county_check
							holder = {
								highest_held_title_tier = tier_county
							}
						}
						alternative_limit = {
							tier = tier_county
							culture = scope:culture_check_2
							valid_county_to_usurp = yes
							save_temporary_scope_as = county_check
						}
						holder = {
							save_scope_as = location_target
							create_character = {
								template = peasant_character
								dynasty = none
								culture = prev.culture
								faith = prev.faith
								location = scope:location_target.location
								gender_female_chance = {
									if = {
										limit = {
											prev.faith = { has_doctrine = doctrine_gender_male_dominated }
										}
										add = 0
									}
									else_if = {
										limit = {
											prev.faith = { has_doctrine = doctrine_gender_female_dominated }
										}
										add = 100
									}
									else = {
										add = 50
									}
								}
								save_scope_as = diverge_dummy
							}
						}
						create_title_and_vassal_change = {
							type = usurped
							save_scope_as = change
							add_claim_on_loss = no
						}
						change_title_holder = {
							holder = scope:diverge_dummy
							change = scope:change
							take_baronies = no
						}
						resolve_title_and_vassal_change = scope:change
					}
				}
				scope:diverge_dummy = {
					create_divergent_culture_with_side_effects_excluding_cost = yes
					trigger_event = culture_emergence_events.0004 # Jump to the next event to make sure the new culture is resolved
				}
			}
		}
	}

	#option = {
	#	name = culture_emergence_events.0002.a
	#
	#	ai_chance = {
	#		base = 100
	#	}
	#}
}

scripted_effect try_to_expand_one_more_duchy_effect = {
	random_in_de_jure_hierarchy = {
		limit = {
			tier = tier_county
			any_neighboring_county = {
				duchy = {
					any_in_de_jure_hierarchy = {
						count >= 2
						tier = tier_county
						any_neighboring_county = {
							count >= 1
							NOT = {
								culture = scope:culture_check_2
							}
							culture = {
								culture_age >= culture_divergence_cooldown
							}
						}
						culture = scope:culture_check_2
						valid_county_to_usurp = yes
					}
					NAND = { # The Cultural Heads capital duchy is protected
						exists = scope:culture_check_2.culture_head
						any_in_de_jure_hierarchy = {
							continue = { tier = tier_duchy }
							tier = tier_county
							holder = scope:culture_check_2.culture_head
							this = holder.capital_county
						}
					}
				}
			}
		}
		random_neighboring_county = {
			limit = {
				duchy = {
					any_in_de_jure_hierarchy = {
						count >= 2
						tier = tier_county
						any_neighboring_county = {
							count >= 1
							NOT = {
								culture = scope:culture_check_2
							}
							culture = {
								culture_age >= culture_divergence_cooldown
							}
						}
						culture = scope:culture_check_2
						valid_county_to_usurp = yes
					}
					NAND = { # The Cultural Heads capital duchy is protected
						exists = scope:culture_check_2.culture_head
						any_in_de_jure_hierarchy = {
							continue = { tier = tier_duchy }
							tier = tier_county
							holder = scope:culture_check_2.culture_head
							this = holder.capital_county
						}
					}
				}
			}
			duchy = {
				save_temporary_scope_as = extra_duchy_check
				every_in_de_jure_hierarchy = {
					limit = {
						tier = tier_barony
						county = {
							OR = {
								culture = scope:culture_check_2
								culture = scope:diverge_dummy.culture
							}
						}
					}
					if = {
						limit = {
							exists = holder
							holder = {
								culture = scope:culture_check_2
								highest_held_title_tier = tier_barony
							}
						}
						holder = {
							set_culture = scope:diverge_dummy.culture
							every_courtier_or_guest = {
								limit = {
									culture = scope:culture_check_2
									is_ai = yes
								}
								set_culture = scope:diverge_dummy.culture
								hidden_effect = {
									add_character_flag = converted_culture_this_lifetime
								}
							}
							if = {
								limit = {
									exists = capital_province
								}
								every_pool_character = {
									province = capital_province
									limit = {
										culture = scope:culture_check_2
										is_ai = yes
									}
									set_culture = scope:diverge_dummy.culture
									hidden_effect = {
										add_character_flag = converted_culture_this_lifetime
									}
								}
							}
						}
					}
				}
				every_in_de_jure_hierarchy = {
					limit = {
						tier = tier_county
						culture = scope:culture_check_2
					}
					if = {
						limit = {
							exists = holder
							holder.capital_county.duchy = scope:extra_duchy_check
							holder = { # duplicates what's in holder_valid_to_diverge as the scopes change (scope:duchy to scope:extra_duchy_check)
								culture = scope:culture_check_2
								capital_county = {
									duchy = scope:extra_duchy_check
								}
								NOT = {
									any_sub_realm_county = {
										culture = scope:culture_check_2
										NOT = {
											duchy = scope:extra_duchy_check
										}
									}
								}
								highest_held_title_tier < tier_empire # Not emperors, though
							}
						}
						holder = {
							set_culture = scope:diverge_dummy.culture
							every_courtier_or_guest = {
								limit = {
									culture = scope:culture_check_2
									is_ai = yes
								}
								set_culture = scope:diverge_dummy.culture
								hidden_effect = {
									add_character_flag = converted_culture_this_lifetime
								}
							}
							if = {
								limit = {
									exists = capital_province
								}
								every_pool_character = {
									province = capital_province
									limit = {
										culture = scope:culture_check_2
										is_ai = yes
									}
									set_culture = scope:diverge_dummy.culture
									hidden_effect = {
										add_character_flag = converted_culture_this_lifetime
									}
								}
							}
						}
					}
					set_county_culture = scope:diverge_dummy.culture
				}
			}
		}
	}
}

culture_emergence_events.0004 = {
	type = character_event
	hidden = yes
	
	immediate = {
		scope:duchy_title = {
			every_in_de_jure_hierarchy = { # Start by spreading the new culture to all Barons and their Courts, including pool characters
				limit = {
					tier = tier_barony
					county = {
						OR = {
							culture = scope:culture_check_2
							culture = scope:diverge_dummy.culture
						}
					}
				}
				if = {
					limit = {
						exists = holder
						holder = {
							culture = scope:culture_check_2
							highest_held_title_tier = tier_barony
						}
					}
					holder = {
						set_culture = scope:diverge_dummy.culture
						every_courtier_or_guest = {
							limit = {
								culture = scope:culture_check_2
								is_ai = yes
							}
							set_culture = scope:diverge_dummy.culture
							hidden_effect = {
								add_character_flag = converted_culture_this_lifetime
							}
						}
						if = {
							limit = {
								exists = capital_province
							}
							every_pool_character = {
								province = capital_province
								limit = {
									culture = scope:culture_check_2
									is_ai = yes
								}
								set_culture = scope:diverge_dummy.culture
								hidden_effect = {
									add_character_flag = converted_culture_this_lifetime
								}
							}
						}
					}
				}
			}
			every_in_de_jure_hierarchy = { # Then spread it to higher-tiered holders with their capital in the duchy
				limit = {
					tier = tier_county
					OR = {
						culture = scope:culture_check_2
						culture = scope:diverge_dummy.culture
					}
				}
				if = {
					limit = {
						exists = holder
						holder.culture = scope:culture_check_2
						holder.capital_county.duchy = scope:duchy_title
						holder = {
							holder_valid_to_diverge = yes
							highest_held_title_tier < tier_empire # Not emperors, though
						}
					}
					holder = { # Set the holder, their courtiers and pool characters to the new culture
						set_culture = scope:diverge_dummy.culture
						every_courtier_or_guest = {
							limit = {
								culture = scope:culture_check_2
								is_ai = yes
							}
							set_culture = scope:diverge_dummy.culture
							hidden_effect = {
								add_character_flag = converted_culture_this_lifetime
							}
						}
						if = {
							limit = {
								exists = capital_province
							}
							every_pool_character = {
								province = capital_province
								limit = {
									culture = scope:culture_check_2
									is_ai = yes
								}
								set_culture = scope:diverge_dummy.culture
								hidden_effect = {
									add_character_flag = converted_culture_this_lifetime
								}
							}
						}
					}
				}
				set_county_culture = scope:diverge_dummy.culture
			}
			if = { # Try to spread the divergence further, if appropriate
				limit = {
					scope:culture_check_2 = {
						culture_number_of_counties >= 70
					}
				}
				try_to_expand_one_more_duchy_effect = yes
				try_to_expand_one_more_duchy_effect = yes
				try_to_expand_one_more_duchy_effect = yes
			}
			else_if = {
				limit = {
					scope:culture_check_2 = {
						culture_number_of_counties >= 50
					}
				}
				try_to_expand_one_more_duchy_effect = yes
				try_to_expand_one_more_duchy_effect = yes
			}
			else_if = {
				limit = {
					scope:culture_check_2 = {
						culture_number_of_counties >= 30
					}
				}
				try_to_expand_one_more_duchy_effect = yes
			}
		}
		# For testing
		#add_to_global_variable_list = { name = diverged_cultures_from_events target = scope:diverge_dummy.culture }
		#if = {
		#	limit = {
		#		any_in_global_list = {
		#			variable = diverged_cultures_from_events # Error suppression
		#		}
		#	}
		#}
	}
}
